package com.search.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.lang.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.search.dao.repo.HospitalRepository;
import com.search.entity.Hospital;
import com.search.service.HospitalQueueSink;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Service;

import java.text.ParseException;

@Service
@EnableBinding(HospitalQueueSink.class)
public class IngestHospitalService {

    @Autowired
    private HospitalRepository productRepository;

    private static final String DELETE = "bootstrap-delete";
    private static final String INSERT = "bootstrap-insert";
    private static final String UPDATE = "bootstrap-update";

    @StreamListener(HospitalQueueSink.INPUT)
    public void handle(String content) {
        ObjectMapper om = new ObjectMapper();
        // 解析binlog操作
        String operation = null;
        // 将Json解析成JsonNode
        JsonNode jsonNode = null;

        try {
            operation = om.readTree(content).path("type").asText();
            jsonNode = om.readTree(content).path("data");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        if (null != jsonNode) {
            Hospital hospital = new Hospital();
            String brief = ObjectUtil.isNotNull(jsonNode.get("brief")) ? jsonNode.get("brief").asText(): "";
            String address = ObjectUtil.isNotNull(jsonNode.get("address")) ? jsonNode.get("address").asText(): "";
            Integer cityId = ObjectUtil.isNotNull(jsonNode.get("city_id")) ? jsonNode.get("city_id").asInt(): 0;
            Long id = ObjectUtil.isNotNull(jsonNode.get("id")) ? jsonNode.get("id").asLong(): 0;
            String lat = ObjectUtil.isNotNull(jsonNode.get("lat")) ? jsonNode.get("lat").asText(): "";
            String lng = ObjectUtil.isNotNull(jsonNode.get("lng")) ? jsonNode.get("lng").asText(): "";
            String name = ObjectUtil.isNotNull(jsonNode.get("name")) ? jsonNode.get("name").asText(): "";
            String createTime = ObjectUtil.isNotNull(jsonNode.get("create_time")) ? jsonNode.get("create_time").asText(): "";
            String updateTime = ObjectUtil.isNotNull(jsonNode.get("update_time")) ? jsonNode.get("update_time").asText(): "";
            hospital.setBrief(brief);
            hospital.setCityId(cityId);
            hospital.setId(id);
            hospital.setLat(lat);
            hospital.setLng(lng);
            hospital.setAddress(address);
            hospital.setName(name);
            String parsePattern = "yyyy-MM-dd HH:mm:ss";
            try {
                if(StringUtils.isNotEmpty(createTime)){
                    hospital.setCreateTime(DateUtils.parseDate(createTime, parsePattern));
                }
                if(StringUtils.isNotEmpty(updateTime)){
                    hospital.setUpdateTime(DateUtils.parseDate(updateTime, parsePattern));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

            switch (operation) {
                case INSERT:
                case UPDATE:
                    productRepository.save(hospital);
                    break;
                case DELETE:
                    productRepository.deleteById(hospital.getId());
                    break;
                default:
                    break;
            }
        }
    }
}
